﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBAccessLib;

namespace LineWatchSimple.WinForm
{
    public partial class LineInfoSetView : Form
    {
        public ProductManagementView ProductManagementForm;
        private DataTable dtLineInfo = new DataTable();
        private DataTable dtCommentInfo = new DataTable();
        private DataTable dtLineShift = new DataTable();

        public LineInfoSetView()
        {
            InitializeComponent();
            //this.dataGridView1.DataError += delegate(object sender, DataGridViewDataErrorEventArgs e) { };
            //this.dataGridView2.DataError += delegate(object sender, DataGridViewDataErrorEventArgs e) { };
        }


        private void LineInfoSetView_Load(object sender, EventArgs e)
        {
            this.InitLineData();
        }


        private void InitLineData()
        {
            this.dtLineInfo = LineInfo.GetLineInfo(string.Empty);
            this.dtCommentInfo = LineInfo.GetCommentInfo();
            this.dtLineShift = LineShift.GetLineShift(string.Empty);

            this.dataGridView1.DataSource = this.dtLineInfo;
            this.dataGridView2.DataSource = this.dtLineShift;

            if (this.dtCommentInfo.Rows.Count > 0)
            {
                this.textBox1.Text = this.dtCommentInfo.Rows[0]["Comment"].ToString();
            }
            else
            {
                this.textBox1.Text = string.Empty;
            }
        }


        private bool DoValidation()
        {
            foreach (DataRow row in this.dtLineShift.Rows)
            {
                if (row.RowState != DataRowState.Deleted
                    && row.RowState != DataRowState.Detached)
                {
                    string startTime = row["StartTime"].ToString();
                    string endTime = row["EndTime"].ToString();

                    DateTime temp = DateTime.Now;

                    if (string.IsNullOrWhiteSpace(startTime))
                    {
                        MessageBox.Show("班次开始时间不能为空!",
                                        "验证",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Warning);
                        return false;

                    }
                    else
                    {
                        if (!DateTime.TryParse(startTime + ":00", out temp))
                        {
                            MessageBox.Show("班次开始时间不正确：" + startTime,
                                            "验证",
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Warning);
                            return false;
                        }
                    }

                    if (string.IsNullOrWhiteSpace(endTime))
                    {
                        MessageBox.Show("班次结束时间不能为空!",
                                        "验证",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Warning);
                        return false;

                    }
                    else
                    {
                        if (!DateTime.TryParse(endTime + ":59", out temp))
                        {
                            MessageBox.Show("班次结束时间不正确：" + endTime,
                                            "验证",
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Warning);
                            return false;
                        }
                    }
                }
            }

            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.dataGridView1.EndEdit();
            this.dataGridView2.EndEdit();

            if (!this.DoValidation())
            {
                return;
            }

            this.dtCommentInfo.Rows.Clear();

            if (!string.IsNullOrWhiteSpace(this.textBox1.Text.Trim()))
            {
                DataRow row = this.dtCommentInfo.NewRow();
                row["Comment"] = this.textBox1.Text.Trim();
                this.dtCommentInfo.Rows.Add(row);
            }

            bool result1 = LineInfo.UpdateLineInfo(this.dtLineInfo, this.dtLineShift);

            if (this.ProductManagementForm != null)
            {
                this.ProductManagementForm.GetLineInfoAndUpdateUI();
            }

            bool result2 = LineInfo.UpdateCommentInfo(this.dtCommentInfo);

            if (result1 && result2)
            {
                MessageBox.Show("保存成功!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (!result1 && !result2)
            {
                MessageBox.Show("保存失败!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (!result1)
            {
                MessageBox.Show("生产线及班次信息设置保存失败!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (!result2)
            {
                MessageBox.Show("生产线消息设置保存失败!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.InitLineData();
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int RowNumber;
                if (null == dataGridView1.CurrentCell)
                {
                    return;
                }
                RowNumber = dataGridView1.CurrentCell.RowIndex;
                dataGridView1.Rows.RemoveAt(RowNumber);
                bool result1 = LineInfo.UpdateLineInfo(this.dtLineInfo, this.dtLineShift);
                if (!result1)
                {
                    MessageBox.Show("删除失败！！！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除失败！！！");
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int RowNumber;
                if (null == dataGridView2.CurrentCell)
                {
                    return;
                }
                RowNumber = dataGridView2.CurrentCell.RowIndex;
                dataGridView2.Rows.RemoveAt(RowNumber);
                bool result1 = LineInfo.UpdateLineInfo(this.dtLineInfo, this.dtLineShift);
                if (!result1)
                {
                    MessageBox.Show("删除失败！！！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除失败！！！");
            }
        }
    }
}
